'use strict';

goog.provide('Blockly.Arduino.sensors');
goog.require('Blockly.Arduino');

Blockly.Arduino.forBlock['xnr_encoder_setup'] = function() {
    var value_xin_nong_r_x1 = Blockly.Arduino.valueToCode(this, 'xin_nong_r_x1', Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_xin_nong_r_x3 = this.getFieldValue('xin_nong_r_x3');
    var value_xin_nong_r_x2 = Blockly.Arduino.valueToCode(this, 'xin_nong_r_x2', Blockly.Arduino.ORDER_ATOMIC);
    Blockly.Arduino.definitions_['include_'+'RotaryEncoder'] = '#include <RotaryEncoder.h>';
    Blockly.Arduino.definitions_[value_xin_nong_r_x1 + value_xin_nong_r_x2] = 'RotaryEncoder encoder'
                                                                              + dropdown_xin_nong_r_x3
                                                                              + '('
                                                                              + value_xin_nong_r_x1
                                                                              + ','
                                                                              + value_xin_nong_r_x2
                                                                              + ',RotaryEncoder::LatchMode::TWO03);';
    var code = '';
    return code;
};

Blockly.Arduino.forBlock['xnr_encoder_poll'] = function() {
    var dropdown_xin_nong_r_x8 = this.getFieldValue('xin_nong_r_x8');
    var code = 'encoder'
               + dropdown_xin_nong_r_x8
               + '.'
               + 'tick();\n';
    return code;
};

Blockly.Arduino.forBlock['xnr_encoder_get'] = function() {
    var dropdown_xin_nong_r_x6 = this.getFieldValue('xin_nong_r_x6');
    var dropdown_xin_nong_r_x4 = this.getFieldValue('xin_nong_r_x4');
    var code = 'encoder'
               + dropdown_xin_nong_r_x6
               + '.'
               + dropdown_xin_nong_r_x4
               + '()';
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.forBlock['xnr_encoder_setPosition'] = function() {
    var value_xin_nong_r_x5 = Blockly.Arduino.valueToCode(this, 'xin_nong_r_x5', Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_xin_nong_r_x7 = this.getFieldValue('xin_nong_r_x7');
    var code = 'encoder'
               + dropdown_xin_nong_r_x7
               + '.setPosition('
               + value_xin_nong_r_x5
               + ');\n';
    return code;
};

